var optType = 1, optIndex = 0, optRow = null;

var config = {
    table: {
        title: '权限按钮数据列表',
        toolbar:'#table-auth-btn',
        height: kit.wh().height - 40,
        singleSelect: true,
        pagination: false,
        onClickRow:function () {
            optRow = component.datagrid.getRowData('#table-list');
            optIndex = component.datagrid.getDataRow('#table-list');
        },
        columns:[
            [
                {field:'name',title:'按钮名称',align:'center',width:120},
                {field:'fun',title:'执行函数',align:'center',width:150},
                {field:'method',title:'请求类型',align:'center',width:120,
                    formatter:function (val, row, index) {
                        var color = '', txt = '';
                        switch (parseInt(val)){
                            case 1:
                                color = '#2c98f0';
                                break;
                            case 2:
                                color = '#83c486';
                                break;
                            case 3:
                                color = '#785549';
                                break;
                            case 4:
                                color = 'red';
                                break;
                        }
                        $.each(format.get2dict('http-method'), function (index, obj) {
                            if(obj.code == val){
                                txt = '<span style="color:'+color+'">' + obj.name + '</span>';
                            }
                        })
                        return txt;
                    }
                },
                {field:'isBtn',title:'按钮',align:'center',
                    formatter:function (val, row, index) {
                        if(val == 1){
                            return '<span style="color: green;">是</span>'
                        }else{
                            return '<span style="color:red;">否</span>'
                        }
                    }
                },
                {field:'icon',title:'图标',align:'center',
                    formatter:function (val, row, index) {
                        return '<span class="icon icon16 '+val+'">&nbsp;</span>'
                    }
                },
                {field:'sort',title:'排序',align:'center',width:100},
                {field:'remark',title:'备注',align:'left',width:300}
            ]
        ]
    },
    form: {
        title: '字典操作',
        width:450,
        height:450,
        buttons: [{
            text:'保 存',
            iconCls:'icon-accept icon16',
            handler:function(){
                switch (optType){
                    case 1:
                        action.add('#table-list');
                        break;
                    case 2:
                        action.edit(optRow.id, optIndex, '#table-list');
                        break;
                }
            }
        },{
            iconCls:'icon-cancel icon16',
            text:'关 闭',
            handler:function(){
                component.dialog.close('#opt-form');
            }
        }]
    },
    icon: {
        title : '选择图标',
        width : kit.wh().width - 10,
        height : kit.wh().height - 10,
        left : 10,
        buttons : [{
            iconCls:'icon-cancel icon16',
            text:'关 闭',
            handler:function(){
                component.dialog.close('#opt-icon');
            }
        }]
    },
}
$(function () {
    component.datagrid.init('#table-list', config.table);
    action.get();
})
var opt = {
    add: function (_event) {
        config.form.title = '新增权限按钮';
        component.dialog.init('#opt-form', config.form);
        component.dialog.open('#opt-form');
        kit.form.clean('#opt-form-data');
        $('#button-icon').attr('class', '');
        $('#button-icon-val').textbox('setValue', '');
        component.combobox.init('#button-method', format.get2dict('http-method'));
        component.combobox.setValue('#button-method', 1);
        $('[name="isBtn"]:radio').each(function() {
            if (this.value == '1'){
                this.checked = true;
            }
        });
        optType = 1;
    },
    edit: function (_event) {
        if(optRow == null){
            kit.alert('请先选择一条数据', 'warning');
            return false;
        }
        config.form.title = '修改权限按钮';
        component.dialog.init('#opt-form', config.form);
        component.dialog.open('#opt-form');
        kit.form.set('#opt-form-data', optRow);
        $('#button-icon').attr('class', 'icon icon16 ' + optRow.icon);
        $('#button-icon-val').textbox('setValue', optRow.icon);
        kit.render('#button-icon');
        component.combobox.init('#button-method', format.get2dict('http-method'));
        component.combobox.setValue('#button-method', optRow.method);
        $('[name="isBtn"]:radio').each(function() {
            if (this.value == optRow.isBtn){
                this.checked = true;
            }
        });
        optType = 2;
    },
    delete: function (_event) {
        if(optRow == null){
            kit.alert('请先选择一条数据', 'warning');
            return false;
        }
        optType = 9;
        var fun = "action.delete();";
        var c = {title: '删除权限', message: '确定要删除这个权限按钮吗？', submit: fun }
        kit.confirm(c);
    },
    icon: function (_event) {
        component.dialog.init('#opt-icon', config.icon);
        component.dialog.open('#opt-icon');
    },
    getIcon: function (_event) {
        var icon = $(_event).attr('class');
        $('#button-icon').attr('class', 'icon icon16 '+ icon);
        $('#button-icon-val').textbox('setValue', icon);
        component.dialog.close('#opt-icon');
    }
}
var action = {
    get: function () {
        var param = {
            url: constant.route.button
        }
        var request = http.get(param);
        request.done(function (d) {
            var data = d.result;
            component.datagrid.load('#table-list', data);
        })
    },
    add:function () {
        var data = kit.form.get('#opt-form-data');
        data['icon'] = $('#button-icon-val').textbox('getValue');
        var param = {
            url : constant.route.button,
            data : data
        }
        var request = http.post(param);
        request.done(function (d) {
            var data = d.result;
            kit.form.clean('#opt-form-data');
            $('#button-icon').attr('class', '');
            $('#button-icon-val').textbox('setValue', '');
            kit.alert(d.message, 'success');
            component.datagrid.append('#table-list', data);
        })
    },
    edit:function () {
        var data = kit.form.get('#opt-form-data');
        data['id'] = optRow.id;
        data['icon'] = $('#button-icon-val').textbox('getValue');
        var param = {
            url: constant.route.button,
            data: data
        }
        var request = http.put(param);
        request.done(function (d) {
            var data = d.result;
            kit.alert(d.message, 'success');
            component.datagrid.updateRow('#table-list', optIndex, data);
            component.dialog.close('#opt-form');
        })
    },
    delete:function () {
        var param = {
            url: constant.route.button + '/' + optRow.id
        }
        var request = http.delete(param);
        request.done(function (d) {
            kit.alert(d.message, 'success');
            component.datagrid.deleteRow('#table-list', optIndex);
            optRow = null;
        })
    },
}